package com.hongxia.assetsys.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hongxia.assetsys.common.ErrorCode;
import com.hongxia.assetsys.common.ResultUtil;
import com.hongxia.assetsys.domain.AssertOrg;
import com.hongxia.assetsys.domain.SysUser;
import com.hongxia.assetsys.domain.TaskAttack;
import com.hongxia.assetsys.exception.BusinessException;
import com.hongxia.assetsys.service.SysUserService;
import com.hongxia.assetsys.service.TaskAttackService;
import com.hongxia.assetsys.mapper.TaskAttackMapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;

/**
* @author Hongxia
* @description 针对表【tb_task_attack】的数据库操作Service实现
* @createDate 2024-01-06 10:34:10
*/
@Service
@Slf4j
public class TaskAttackServiceImpl extends ServiceImpl<TaskAttackMapper, TaskAttack>
implements TaskAttackService {
    @Resource
    TaskAttackMapper taskAttackMapper;
    @Resource
    SysUserService userService;

    @Override
    public Integer addTaskAttack(TaskAttack task) {
        if(task == null){
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        String taskname = task.getTaskname();
        if(StringUtils.isBlank(taskname)){
            throw new BusinessException(ErrorCode.PARAMS_ERROR,"任务名称为空");
        }
        //设置任务状态
        task.setTaskstatus("未开始");
        boolean result = this.save(task);
        if(!result){
            throw new BusinessException(ErrorCode.SYSTEM_ERROR,"保存失败！");
        }
        return task.getId();
    }

    @Override
    public List<TaskAttack> getTaskByTaskNameLikeyly(String taskname) {
        if(taskname == null){
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        QueryWrapper queryWrapper = new QueryWrapper<>();
        queryWrapper.like("taskName","%" + taskname + "%");
        List<TaskAttack> taskList= taskAttackMapper.selectList(queryWrapper);
        return taskList;
    }

    @Override
    public List<TaskAttack> getTaskByTaskDescLikeyly(String taskdesc) {
        if(taskdesc == null){
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        QueryWrapper queryWrapper = new QueryWrapper<>();
        queryWrapper.like("taskDesc","%" + taskdesc + "%");
        List<TaskAttack> taskList= taskAttackMapper.selectList(queryWrapper);
        return taskList;
    }

    @Override
    public List<TaskAttack> getTaskByStatus(String taskstatus) {
        if(taskstatus == null){
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        QueryWrapper queryWrapper = new QueryWrapper<>();
        queryWrapper.like("taskstatus",taskstatus);
        List<TaskAttack> taskList= taskAttackMapper.selectList(queryWrapper);
        return taskList;
    }

    @Override
    public boolean changeTaskStatus(String taskstatus,Integer id) {
        if(taskstatus == null){
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        if(StringUtils.isBlank(taskstatus)){
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        QueryWrapper<TaskAttack> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id",id);
        int count = this.count(queryWrapper);
        if(count <= 0){
            throw new BusinessException(ErrorCode.PARAMS_ERROR,"演练任务不存在");
        }
        // 使用 UpdateWrapper 进行更安全的更新
        UpdateWrapper<TaskAttack> updateWrapper = new UpdateWrapper<>();
        updateWrapper.set("taskstatus", taskstatus).eq("id", id);
        // 更新任务状态
        boolean result = this.update(updateWrapper);
        if(!result){
            throw new BusinessException(ErrorCode.SYSTEM_ERROR,"更新失败");
        }
        return result;
    }

    @Override
    public boolean beginTask(Integer id) {
        if(id == null){
            throw new BusinessException(ErrorCode.NULL_ERROR);
        }
        //修改任务状态
        boolean result = this.changeTaskStatus("进行中", id);
        return result;
    }

    @Override
    public boolean endTask(Integer id) {
        if(id == null){
            throw new BusinessException(ErrorCode.NULL_ERROR);
        }
        boolean result = this.changeTaskStatus("已完成", id);
        return result;
    }

    @Override
    public boolean assignTask(Integer id, String tester) {
        if(id == null || id < 0){
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        if(StringUtils.isBlank(tester)){
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        //校验任务是否存在
        QueryWrapper<TaskAttack> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id",id);
        TaskAttack task = this.getById(id);
        if(task == null){
            throw new BusinessException(ErrorCode.PARAMS_ERROR,"演练任务不存在");
        }
        //校验任务状态
        String taskstatus = task.getTaskstatus();
        if(taskstatus != null && taskstatus.equals("未开始")){
            throw new BusinessException(ErrorCode.PARAMS_ERROR,"演练任务未开始");
        }
        if(taskstatus != null && taskstatus.equals("已完成")){
            throw new BusinessException(ErrorCode.PARAMS_ERROR,"演练任务已完成");
        }
        //校验tester是否存在
        QueryWrapper<SysUser> queryWrapper2 = new QueryWrapper<>();
        queryWrapper2.eq("username",tester);
        int count = userService.count(queryWrapper2);
        if(count <= 0){
            throw new BusinessException(ErrorCode.PARAMS_ERROR,"测试人员不存在");
        }
        // 使用 UpdateWrapper 进行更安全的更新
        UpdateWrapper<TaskAttack> updateWrapper = new UpdateWrapper<>();
        updateWrapper.set("joinedTesters", tester).eq("id", id);
        // 更新任务状态
        boolean result = this.update(updateWrapper);
        if(!result){
            throw new BusinessException(ErrorCode.SYSTEM_ERROR,"更新失败");
        }
        return result;
    }
}
